package hos.java.compat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import hos.java.util.AnyUtils;


/**
 * <p>Title: CalendarConvert </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2021/8/27 15:45
 */
public class CalendarConvert {

    private static SimpleDateFormat getDefaultFormat() {
        return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @SuppressWarnings("SimpleDateFormat")
    public static SimpleDateFormat getSafeDateFormat(String pattern) {
        return new SimpleDateFormat(pattern);
    }

    /**
     * Return the current time in milliseconds.
     *
     * @return the current time in milliseconds
     */
    public static long getNowMills() {
        return System.currentTimeMillis();
    }

    /**
     * Return the current formatted time string.
     * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
     *
     * @return the current formatted time string
     */
    public static String getNowString() {
        return getNowString("yyyy-MM-dd HH:mm:ss");
    }


    //// 获取当前的时间 Now Date Str.(yyyy-MM-dd 00:00:00)
    public static String getNowStringStart() {
        return getNowString("yyyy-MM-dd") + " 00:00:00";
    }

    //// 获取当前的时间 Now Date Str.(yyyy-MM-dd 00:00:00)
    public static String getNowStringEnd() {
        return getNowString("yyyy-MM-dd") + " 23:59:59";
    }

    /**
     * Return the current formatted time string.
     *
     * @param format The format.
     * @return the current formatted time string
     */
    public static String getNowString(final String format) {
        return millis2String(System.currentTimeMillis(), format);
    }

    public static String toEmpty(CharSequence target) {
        String time = AnyUtils.empty(target);
        if (time.isEmpty()) {
            return "";
        }
        if (!time.contains("T") && !time.contains("+") && !time.contains(".")) {
            return time;
        }
        if (time.contains("T")) {
            time = time.replace("T", " ");
        }
        if (time.contains(".")) {
            time = time.substring(0, time.lastIndexOf("."));
        }
        if (time.contains("+")) {
            time = time.substring(0, time.lastIndexOf("+"));
        }
        return time;
    }


    /**
     * Formatted time string to the milliseconds.
     *
     * @param time The formatted time string.
     * @return the milliseconds
     */
    public static long string2Millis(final CharSequence time) {
        return string2Millis(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time    The formatted time string.
     * @param pattern The pattern of date format, such as yyyy-MM-dd HH:mm:ss
     * @return the milliseconds
     */
    public static long string2Millis(final CharSequence time, final String pattern) {
        String empty = toEmpty(time);
        if (empty.isEmpty()) {
            return -1;
        }
        try {
            return string2Millis(empty, getSafeDateFormat(pattern));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static long string2Millis(String time, DateFormat format) {
        try {
            return format.parse(time).getTime();
        } catch (ParseException var3) {
            var3.printStackTrace();
            return -1L;
        }
    }

    public static String millis2String(long millis, DateFormat format) {
        return format.format(new Date(millis));
    }

    /**
     * Formatted time string to the milliseconds.
     * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
     *
     * @param time The formatted time string.
     * @return the milliseconds
     */

    public static Calendar string2Calendar(final String time) {
        return string2Calendar(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time    The formatted time string.
     * @param pattern The pattern of date format, such as yyyy-MM-dd HH:mm:ss
     * @return the milliseconds
     */

    public static Calendar string2Calendar(final String time, final String pattern) {
        long string2Millis = string2Millis(time, pattern);
        if (string2Millis == -1) {
            return new GregorianCalendar();
        }
        return CalendarCompat.getCalendar(string2Millis);
    }


    /**
     * Milliseconds to the formatted time string.
     * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
     *
     * @param millis The milliseconds.
     * @return the formatted time string
     */
    public static String millis2String(final long millis) {
        return millis2String(millis, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis  The milliseconds.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the formatted time string
     */
    public static String millis2String(long millis, final String pattern) {
        return millis2String(millis,getSafeDateFormat(pattern));
    }

    public static String millis2StringStart(long millis) {
        return millis2String(millis, "yyyy-MM-dd") + " 00:00:00";
    }

    public static String millis2StringEnd(long millis) {
        return millis2String(millis, "yyyy-MM-dd") + " 23:59:59";
    }

    public static Calendar millis2Calendar(Long millis) {
        Calendar calendar = new GregorianCalendar();
        if (millis == null) {
            return calendar;
        }
        calendar.setTimeInMillis(millis);
        return calendar;
    }

    /**
     * Milliseconds to the formatted time string.
     * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
     *
     * @param calendar The milliseconds.
     * @return the formatted time string
     */
    public static String calendar2String(final Calendar calendar) {
        return calendar2String(calendar, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param calendar The milliseconds.
     * @param pattern  The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the formatted time string
     */
    public static String calendar2String(final Calendar calendar, final String pattern) {
        return millis2String(calendar.getTimeInMillis(), pattern);
    }


    public static String calendar2StringStart(Calendar calendar) {
        return calendar2String(calendar, "yyyy-MM-dd") + " 00:00:00";
    }

    public static String calendar2StringEnd(Calendar calendar) {
        return calendar2String(calendar, "yyyy-MM-dd") + " 23:59:59";
    }

    public static String convertTime(CharSequence target) {
        return convertTime(target, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd");
    }

    public static String convertTime(CharSequence target, String to) {
        return convertTime(target, "yyyy-MM-dd HH:mm:ss", to);
    }

    public static String convertTime(CharSequence target, String from, String to) {
        long time = string2Millis(target, from);
        if (time == -1L) {
            return "";
        }
        return millis2String(time, to);
    }

    /**
     * 获取当前周的星期日，在当前月中
     *
     * @param week 第几周
     * @param time 时间
     * @return
     */
    public static String getWeekSundayForMonth(int week, String time) {
        return getWeekSundayForMonth(week, time, CalendarFormat.full);
    }

    /**
     * 获取当前周的星期日，在当前月中
     *
     * @param week   第几周
     * @param time   时间
     * @param format 时间格式
     * @return
     */
    public static String getWeekSundayForMonth(int week, String time, String format) {
        return getWeekSundayForMonth(week, time, format, format);
    }

    /**
     * 获取当前周的星期日，在当前月中
     *
     * @param week      第几周
     * @param time      时间
     * @param inFormat  输入时间格式
     * @param outFormat 输出时间格式
     * @return
     */
    public static String getWeekSundayForMonth(int week, String time, String inFormat, String outFormat) {
        return calendar2String(
                CalendarCompat.getWeekSundayForMonth(
                        week,
                        string2Millis(time, inFormat)
                ), outFormat
        );
    }

    /**
     * 获取当前周的星期日，在当前月中
     *
     * @param week   第几周
     * @param time   时间
     * @param format 时间格式
     * @return
     */
    public static String getWeekMondayForMonth(int week, String time, String format) {
        return getWeekMondayForMonth(week, time, format, format);
    }

    /**
     * 获取当前周的星期日，在当前月中
     *
     * @param week      第几周
     * @param time      时间
     * @param inFormat  输入时间格式
     * @param outFormat 输出时间格式
     * @return
     */
    public static String getWeekMondayForMonth(int week, String time, String inFormat, String outFormat) {
        return calendar2String(
                CalendarCompat.getWeekMondayForMonth(
                        week,
                        string2Millis(time, inFormat)
                ), outFormat
        );
    }

    public static String formatDurationTime(long duration) {
        return String.format(Locale.getDefault(), "%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(duration),
                TimeUnit.MILLISECONDS.toSeconds(duration)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
    }
}
